package org.ns.summer.example;

import org.ns.summer.beans.anns.Service;
import org.ns.summer.data.mybatis.DBSource;
import org.ns.summer.data.mybatis.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 用户账户服务
 */
@Service
public class UserService {

    private Logger log = LoggerFactory.getLogger(getClass());
    @DBSource("userdb")
    private UserAmountMapper userAmountMapper;
    @DBSource("userdb")
    private UserInfoMapper userInfoMapper;

    @Transaction
    public void transferAccounts(long userId, long recvUserId, long money) {
        System.out.println(userAmountMapper.addMoney(recvUserId, money));
        System.out.println(userAmountMapper.subMoney(userId, money));
        log.debug(String.format("%d,%d,%d", userId, recvUserId, money));
    }

    @Transaction
    public boolean createUser(long userId, String userName) {
        if (userInfoMapper.exist(userId) > 0){
            return false;
        }
        userInfoMapper.insert(userId, userName);
        userAmountMapper.insert(userId, 10000); //
        return true;
    }

    @Transaction
    public void delete(long userId) {
        userInfoMapper.deleteByUserId(userId);
        userAmountMapper.delete(userId);
    }

    public String getUserName(long userId) {
        return userInfoMapper.getUserName(userId);
    }

    public Long getMoney(long userId) {
        return userAmountMapper.getMoney(userId);
    }


}
